﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.ComControls;
using MINERP.MODEL.ExportPhoto;
using MINERP.BLL.ExportPhoto;
using MINERP.ExportPhoto.Helper;
using System.Threading;
using Leadtools.Codecs;
using MINERP.MODEL.FoundationModel;
using Leadtools;
using Leadtools.ImageProcessing;
using Leadtools.SpecialEffects;
using System.IO;
using MINERP.COM;


namespace MINERP.ExportPhoto
{
    public partial class FormMain : FrmParent
    {
        public FormMain()
        {
            InitializeComponent();
            _SyncContext = SynchronizationContext.Current;
        }

        ExportPhotoBLL _bll = new ExportPhotoBLL();
        private SynchronizationContext _SyncContext;
        private Graphics gFont;
        private Font textFont = new Font("宋体", 25);
        private Font textFontSmall = new Font("宋体", 25);
        Font textBarFont = new Font("C39HrP48DmTt", 50);

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FrmSetting frm = new FrmSetting();
            frm.ShowDialog();
        }

        private void backgroundWorkerAddRemark_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                //ExportTaskInfo
            }
        }

        private void backgroundWorkerAddRemark_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorkerAddRemark_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            gFont = this.CreateGraphics();
            RasterCodecs.Startup();
            ThreadEx _procrssPhoto = ThreadEx.StartTheadNow(ProcessPhoto, 1000*5);
            _procrssPhoto.Start(null);
            //ThreadEx _rushWaitList = ThreadEx.StartThead(RushListToDown, 1000 * 20);
            //_rushWaitList.Start(null);

        }

        void TelTextInfo(string inText)
        {
            richTextBoxInfo.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "：" + inText + Environment.NewLine +
                                   richTextBoxInfo.Text;

        }

        void TellInfo(string str)
        {
            tsInfo.Text = str;
        }

        void TellProcrss(int value)
        {
            try
            {
                ProgressProcess.Value = value;
            }
            catch (Exception ex)
            {
                
               
            }
          
        }
        private int ProcessPhoto()
        {
            _SyncContext.Post(c =>
            {
                TellProcrss(1);
            }, null);
            int rtn = 0;//1立即执行0暂停几秒
            ExportTaskInfo info = _bll.GetOneWait();
            if (info == null)
            {
                _SyncContext.Post(c =>
                {
                    TellInfo("等待订单");
                }, null);
                rtn = 0;
                return rtn;
            }
            if(info.PhotoNum<=0)
            {
                _SyncContext.Post(c =>
                {
                    TellInfo("错误订单-" + info.OrderProductId + "\\" + info.OrderProductName);
                    TelTextInfo("错误订单,请及时处理,订单中不含照片,或者照片无法导出," + info.OrderProductId + "\\" + info.OrderProductName);
                }, null);
                rtn = 0;
                return rtn;
            }
            _SyncContext.Post(c =>
                {
            TelTextInfo("等到处理订单：" + info.OrderCount + "," + "当前处理：" + info.OrderProductId + "\\" + info.OrderProductName);
                }, null);
            int step = 100 / info.PhotoNum;
            RasterCodecs codecs = new RasterCodecs();
            int barHeigthmm = Properties.Settings.Default.BarCodeHeigth;
            int barCodeSize = Properties.Settings.Default.BarcodeSize;
            int fontSize = Properties.Settings.Default.FontSize;
            //barLocal = Properties.Settings.Default.BarLocationType;
            textFont = new Font("宋体", fontSize);
            textFontSmall = new Font("宋体", fontSize / 2-1);
            textBarFont = new Font("C39HrP48DmTt", barCodeSize);
            int barLocal = Properties.Settings.Default.BarLocationType;//0上1下2左3右
            int AutoSizeInch = Properties.Settings.Default.AutoSizeInch;
            string strSaveFolder = Properties.Settings.Default.SaveFolder;
            if (string.IsNullOrEmpty(strSaveFolder))
            {
                _SyncContext.Post(c =>
                {
                    TellInfo("请设置文件保存目录");
                    TelTextInfo("请设置文件保存目录");
                }, null);
                rtn = 0;
                return rtn;
            }

            string SaveFilePath = Properties.Settings.Default.SaveFolder + "\\" + info.OrderProductId+"-" + info.ShopName+"-"+ info.CtmName+"-" + info.OrderProductName;
            WriteLogForCheck.WriteLogFile(SaveFilePath);
            if (!Directory.Exists(SaveFilePath))
            {
                Directory.CreateDirectory(SaveFilePath);
            }
            _SyncContext.Post(c =>
            {
                TellInfo("正在处理-" + info.OrderProductId + "\\" + info.OrderProductName);
            }, null);
            int iCount = 0;
            foreach (PhotoInfo_Info picInfo in info.listPhoto)
            {
                iCount++;
                _SyncContext.Post(c =>
                {
                    TellProcrss(iCount * step);
                }, null);
                string fileName = Path.GetFileName(picInfo.PicPath);
                string picSavePath = SaveFilePath + "\\" + fileName;

                int BARCODEWIDTH = 0;
                //原图
                RasterImage srcImg = codecs.Load(picInfo.PicPath);
                if (barLocal == 0 || barLocal == 1)
                {
                    decimal heigth = (decimal)srcImg.Height / srcImg.YResolution;
                    if (heigth >= 29.88m)
                    {
                        barLocal = 3;
                    }
                }
                else
                {
                    decimal widthh = (decimal)srcImg.Width / srcImg.XResolution;
                    if (widthh >= 29.88m)
                    {
                        barLocal = 1;
                    }
                }
                if (srcImg.BitsPerPixel == 8)//判断是否灰度
                {
                    ColorResolutionCommand colorRes = new ColorResolutionCommand();
                    colorRes.Mode = ColorResolutionCommandMode.InPlace;
                    colorRes.BitsPerPixel = 24;
                    colorRes.DitheringMethod = RasterDitheringMethod.None;
                    colorRes.PaletteFlags = ColorResolutionCommandPaletteFlags.Fixed;
                    colorRes.Colors = 0;
                    colorRes.Run(srcImg);


                }
                int barHeigthPix = Convert.ToInt32((double)barHeigthmm / 10 / 2.54 * srcImg.XResolution);
                //目标图
                RasterImage decImg = null;
                //条码底图
                RasterImage barBaseImg = null;
                CreateDestImg(srcImg, barLocal, barHeigthPix, ref decImg, ref barBaseImg);
                DrowBordLine(ref srcImg, Color.Green);//照片本图上画一条绿色线
                RasterImage ColorImgBlock = GetColorBlock(srcImg,info.OrderProductId, 40, barBaseImg.Height);
                #region//条码信息处理
                Graphics gBarcode = barBaseImg.CreateGdiPlusGraphics().Graphics;//用来打印条码和文字
                SpecialEffectsProcessor processor = new SpecialEffectsProcessor();
                //int recX = ColorImgBlock.Width + 10, recY = 0;
                int recX = ColorImgBlock.Width, recY = 0;//当前操作在条码图片上的位置

                SizeF sizeF = gFont.MeasureString("*" + picInfo.PhotoId.ToString() + "*", textBarFont);
                //sizeF.Width/gFont.DpiX*barcodeImageTemp.XResolution;
                BARCODEWIDTH = Convert.ToInt32(sizeF.Width / gFont.DpiX * barBaseImg.XResolution) + 5;//条码的宽度

                bool isPrintBarCode = false;//是否要打印条码

                isPrintBarCode = BARCODEWIDTH + 100 <= barBaseImg.Width;


                //if (BARCODEWIDTH + 100 <= barcodeImageTemp.Width)//条码的长度会不会小于照片某片的长度
                if (isPrintBarCode) //如果照片的宽度大于5寸才打印条码,目的是为了显示更多的有用的信息
                {
                    //打条码
                    Rectangle rc = new Rectangle(recX, recY, BARCODEWIDTH, barBaseImg.Height);


                    gBarcode.DrawString("*" + picInfo.PhotoId.ToString() + "*", textBarFont, Brushes.Black, recX, recY);
                    recX += BARCODEWIDTH;


                }
                gBarcode.Dispose();
                ColorResolutionCommand command = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 24, RasterByteOrder.Bgr,
                                                    RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.Optimized, null);
                command.Run(barBaseImg);

                #endregion
                #region//粘贴色块
                CombineFastCommand cfcBarcode1 = new CombineFastCommand();
                cfcBarcode1.DestinationRectangle = new Rectangle(
                 0,
                 0,
                 ColorImgBlock.Width,
                 ColorImgBlock.Height);

                cfcBarcode1.SourcePoint = Point.Empty;
                cfcBarcode1.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;

                cfcBarcode1.DestinationImage = barBaseImg;
                cfcBarcode1.Run(ColorImgBlock);
                #endregion
                #region//文字信息
                Graphics gText = barBaseImg.CreateGdiPlusGraphics().Graphics;
                //recX += 10;
                Rectangle rcFont = new Rectangle(recX, 0, barBaseImg.Width - recX, barBaseImg.Height);

                string barStr = info.OrderProductId+"\\"+info.ShopName + "\\" + info.OrderProductName + "\\" + picInfo.Remark;
                if (!isPrintBarCode)
                {
                    barStr = picInfo.PhotoId + "\\" + barStr;
                }

                //自使用字号
                if (AutoSizeInch > (float)barBaseImg.Width / barBaseImg.XResolution)
                {
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Near;

                    gText.DrawString(barStr, textFontSmall, new SolidBrush(Color.Black), rcFont, sf);
                }
                else
                {
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Near;
                    gText.DrawString(barStr, textFont, new SolidBrush(Color.Black), rcFont, sf);
                }
                ColorResolutionCommand gTextCommandommand = new ColorResolutionCommand(ColorResolutionCommandMode.InPlace, 24, RasterByteOrder.Bgr,
                                                   RasterDitheringMethod.None, ColorResolutionCommandPaletteFlags.Optimized, null);
                gTextCommandommand.Run(barBaseImg);
                gText.Dispose();
                ColorImgBlock.Dispose();//释放色块的资源
                #endregion
                #region//添加到目标图decImg上
                if (barLocal == 0)
                {
                    Rectangle barCodeRec = new Rectangle();
                    Rectangle picRec = new Rectangle();
                    picRec = new Rectangle(0, barBaseImg.Height, srcImg.Width, srcImg.Height);
                    barCodeRec = new Rectangle(0, 0, barBaseImg.Width, barBaseImg.Height);
                    //选转180°
                    //RotateCommand rcBarcode = new RotateCommand();//旋转条码图片               
                    //rcBarcode.Angle = 180 * 100;
                    //rcBarcode.FillColor = new RasterColor(255, 255, 255);
                    //rcBarcode.Flags = RotateCommandFlags.Resize | RotateCommandFlags.Bicubic;
                    //rcBarcode.Run(barBaseImg);

                    CombineFastCommand comPic = new CombineFastCommand();
                    comPic.SourcePoint = Point.Empty;
                    comPic.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = picRec;
                    comPic.Run(srcImg);
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = barCodeRec;
                    comPic.Run(barBaseImg);

                }
                else if (barLocal == 1)
                {
                    Rectangle barCodeRec = new Rectangle();
                    Rectangle picRec = new Rectangle();
                    picRec = new Rectangle(0,0, srcImg.Width, srcImg.Height);
                    barCodeRec = new Rectangle(0, srcImg.Height, barBaseImg.Width, barBaseImg.Height);

                    
                    CombineFastCommand comPic = new CombineFastCommand();
                    comPic.SourcePoint = Point.Empty;
                    comPic.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = picRec;
                    comPic.Run(srcImg);
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = barCodeRec;
                    comPic.Run(barBaseImg);
                }
                else if (barLocal == 2)
                {
                    Rectangle barCodeRec = new Rectangle();
                    Rectangle picRec = new Rectangle();
                    picRec = new Rectangle(barHeigthPix, 0, srcImg.Width, srcImg.Height);
                    barCodeRec = new Rectangle(0, 0, barBaseImg.Height, barBaseImg.Width);
                    //选转90°
                    RotateCommand rcBarcode = new RotateCommand();//旋转条码图片               
                    rcBarcode.Angle = 90 * 100;
                    rcBarcode.FillColor = new RasterColor(255, 255, 255);
                    rcBarcode.Flags = RotateCommandFlags.Resize | RotateCommandFlags.Bicubic;
                    rcBarcode.Run(barBaseImg);

                    CombineFastCommand comPic = new CombineFastCommand();
                    comPic.SourcePoint = Point.Empty;
                    comPic.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = picRec;
                    comPic.Run(srcImg);
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = barCodeRec;
                    comPic.Run(barBaseImg);
                }
                else if (barLocal == 3)
                {
                    Rectangle barCodeRec = new Rectangle();
                    Rectangle picRec = new Rectangle();
                    picRec = new Rectangle(0, 0, srcImg.Width, srcImg.Height);
                    barCodeRec = new Rectangle(srcImg.Width, 0, barBaseImg.Height, barBaseImg.Width);
                    //选转90°
                    RotateCommand rcBarcode = new RotateCommand();//旋转条码图片               
                    rcBarcode.Angle = 90 * 100;
                    rcBarcode.FillColor = new RasterColor(255, 255, 255);
                    rcBarcode.Flags = RotateCommandFlags.Resize | RotateCommandFlags.Bicubic;
                    rcBarcode.Run(barBaseImg);

                    CombineFastCommand comPic = new CombineFastCommand();
                    comPic.SourcePoint = Point.Empty;
                    comPic.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = picRec;
                    comPic.Run(srcImg);
                    comPic.DestinationImage = decImg;
                    comPic.DestinationRectangle = barCodeRec;
                    comPic.Run(barBaseImg);
                }

                #endregion
                decImg.XResolution = srcImg.XResolution;
                decImg.YResolution = srcImg.XResolution;
                //保存照片
                codecs.Save(decImg, picSavePath, RasterImageFormat.Jpeg, 24);
            }
            bool isComplete=true;
            //验证文件已经存在
            foreach (PhotoInfo_Info picInfo in info.listPhoto)
            {
                string fileName = Path.GetFileName(picInfo.PicPath);
                string picSavePath = SaveFilePath + "\\" + fileName;
                if (!File.Exists(picSavePath))
                {
                    isComplete = false;
                    break;
                }
            }
            if (isComplete)
            {

               //int rtnVal= _bll.CompleteExportPhoto(info, base.userId);
                int rtnVal = _bll.CompleteExportPhoto(info, userId);
                string msg = "";
                if (rtnVal == -3)
                {

                   msg=info.OrderProductId + "\\" + info.OrderProductName+",流程未在导出，请查看产品状态";

                }
                else if (rtnVal == -2)
                {
                    msg=info.OrderProductId + "\\" + info.OrderProductName + ",产品未在生产中，请查看产品状态";

                }
                else if (rtnVal == -1)
                {
                    msg=info.OrderProductId + "\\" + info.OrderProductName + ",产品未找到，请查看产品状态";

                }
                else
                {
                   msg=info.OrderProductId + "\\" + info.OrderProductName + ",导出完成";
 
                }
                _SyncContext.Post(c =>
                {
                    TelTextInfo(msg);
                }, null);
            }
            _SyncContext.Post(c =>
            {
                TellProcrss(100);
            }, null);
            //codes.Load(picInfo.PicPath);
            return rtn;
        }
        private void DrowBordLine(ref  RasterImage rstImg, Color lineColor)
        {
            //RasterCodecs codecs = new RasterCodecs();
            RasterImage leftLine = new RasterImage(
                              RasterMemoryFlags.Conventional,
                              1,//+1
                              rstImg.Height,
                              24,
                              RasterByteOrder.Bgr,
                              RasterViewPerspective.BottomLeft,
                              null,
                              IntPtr.Zero,
                              0);
            FillCommand fcommandW = new FillCommand();
            fcommandW.Color = new RasterColor(lineColor.R, lineColor.G, lineColor.B);
            fcommandW.Run(leftLine);
            RasterImage RightLine = new RasterImage(
                             RasterMemoryFlags.Conventional,
                             1,//+1
                             rstImg.Height,
                             24,
                             RasterByteOrder.Bgr,
                             RasterViewPerspective.BottomLeft,
                             null,
                             IntPtr.Zero,
                             0);

            fcommandW.Color = new RasterColor(lineColor.R, lineColor.G, lineColor.B);
            fcommandW.Run(RightLine);
            RasterImage TopLine = new RasterImage(
                            RasterMemoryFlags.Conventional,
                            rstImg.Width,//+1
                            1,
                            24,
                            RasterByteOrder.Bgr,
                            RasterViewPerspective.BottomLeft,
                            null,
                            IntPtr.Zero,
                            0);
            fcommandW.Color = new RasterColor(lineColor.R, lineColor.G, lineColor.B);
            fcommandW.Run(TopLine);
            RasterImage BottomLine = new RasterImage(
                            RasterMemoryFlags.Conventional,
                            rstImg.Width,//+1
                            1,
                            24,
                            RasterByteOrder.Bgr,
                            RasterViewPerspective.BottomLeft,
                            null,
                            IntPtr.Zero,
                            0);
            fcommandW.Color = new RasterColor(lineColor.R, lineColor.G, lineColor.B);
            fcommandW.Run(BottomLine);
            CombineFastCommand cfcLine = new CombineFastCommand();
            cfcLine.SourcePoint = Point.Empty;
            cfcLine.Flags = CombineFastCommandFlags.OperationAdd | CombineFastCommandFlags.Destination0;
            cfcLine.DestinationImage = rstImg;

            cfcLine.DestinationRectangle = new Rectangle(0, 0, leftLine.Width, leftLine.Height);
            cfcLine.Run(leftLine);

            cfcLine.DestinationRectangle = new Rectangle(rstImg.Width - 1, 0, RightLine.Width, RightLine.Height);
            cfcLine.Run(RightLine);

            cfcLine.DestinationRectangle = new Rectangle(0, 0, TopLine.Width, TopLine.Height);
            cfcLine.Run(TopLine);

            cfcLine.DestinationRectangle = new Rectangle(0, rstImg.Height - 1, BottomLine.Width, BottomLine.Height);
            cfcLine.Run(BottomLine);



        }
        /// <summary>
        /// 创建原图
        /// </summary>
        /// <param name="srcImg"></param>
        /// <param name="barLocal"></param>
        /// <param name="barHeigthPix"></param>
        /// <returns></returns>
        private void CreateDestImg(RasterImage srcImg, int barLocal, int barHeigthPix, ref  RasterImage drcImg, ref RasterImage barcodeImageTemp)
        {

            int barCodeImageWidth;
            int midPicWidth = 0, midPicHeight = 0;
            if (barLocal == 0 || barLocal == 1)
            {
                barCodeImageWidth = srcImg.Width;
                midPicWidth = srcImg.Width;
                midPicHeight = srcImg.Height + barHeigthPix;

            }
            else
            {
                barCodeImageWidth = srcImg.Height;
                midPicWidth = srcImg.Width + barHeigthPix;
                midPicHeight = srcImg.Height;

            }
            drcImg = new RasterImage(RasterMemoryFlags.Conventional, midPicWidth, midPicHeight, 24, RasterByteOrder.Bgr, RasterViewPerspective.BottomLeft, null, IntPtr.Zero, 0);

            FillCommand midfcommand = new FillCommand();//填充画布

            midfcommand.Color = new RasterColor(255, 255, 255);
            midfcommand.Run(drcImg);

            barcodeImageTemp = new RasterImage(
                                                        RasterMemoryFlags.Conventional,
                                                        barCodeImageWidth,
                                                        barHeigthPix,
                                                        1,
                                                        RasterByteOrder.Gray,
                                                        RasterViewPerspective.BottomLeft,
                                                        null,
                                                        IntPtr.Zero,
                                                        0);

            //设置条码图片的DPI这样才能扫描时更精确
            FillCommand fcBarcode = new FillCommand();
            fcBarcode.Color = new RasterColor(255, 255, 255);
            barcodeImageTemp.XResolution = srcImg.XResolution;
            barcodeImageTemp.YResolution = srcImg.YResolution;
            fcBarcode.Run(barcodeImageTemp);
        }
        private RasterImage GetColorBlock(RasterImage srcImg,int orderProductId,int width,int heigth)//根据产品得到相应的色块
        {
            int colorIndex = 0;
            RasterColor curColor;
            colorIndex = orderProductId % 30;
            #region 设定颜色
            switch (colorIndex)
            {
                case 1:
                    curColor = Color1;
                    break;
                case 2: curColor = Color2;
                    break;
                case 3: curColor = Color3;
                    break;
                case 4: curColor = Color4;
                    break;
                case 5: curColor = Color5;
                    break;
                case 6: curColor = Color6;
                    break;
                case 7: curColor = Color7;
                    break;
                case 8: curColor = Color8;
                    break;
                case 9: curColor = Color9;
                    break;
                case 10: curColor = Color10;
                    break;
                case 11: curColor = Color11;
                    break;
                case 12: curColor = Color12;
                    break;
                case 13: curColor = Color13;
                    break;
                case 14: curColor = Color14;
                    break;
                case 15: curColor = Color15;
                    break;
                case 16: curColor = Color16;
                    break;
                case 17: curColor = Color17;
                    break;
                case 18: curColor = Color18;
                    break;
                case 19: curColor = Color19;
                    break;
                case 20: curColor = Color20;
                    break;
                case 21: curColor = Color21;
                    break;
                case 22: curColor = Color22;
                    break;
                case 23: curColor = Color23;
                    break;
                case 24: curColor = Color24;
                    break;
                case 25: curColor = Color25;
                    break;
                case 26: curColor = Color26;
                    break;
                case 27: curColor = Color27;
                    break;
                case 28: curColor = Color28;
                    break;
                case 29: curColor = Color29;
                    break;
                default: curColor = Color30;
                    break;
            }
            #endregion


            RasterImage destImage = new RasterImage(
                    RasterMemoryFlags.Conventional,
                    width,
                    heigth,
                    24,
                    RasterByteOrder.Bgr,
                    RasterViewPerspective.BottomLeft,
                    null,
                    IntPtr.Zero,
                    0);

            FillCommand fcommand = new FillCommand();
            fcommand.Color = curColor;
            fcommand.Run(destImage);
            FillCommand fcBarcode = new FillCommand();
            fcBarcode.Color = new RasterColor(255, 255, 255);
            destImage.XResolution = srcImg.XResolution;
            destImage.YResolution = srcImg.YResolution;
            return destImage;
        }
        public void RushListToDown()
        {
            _SyncContext.Post(c =>
            {
                gridControlTask.DataSource = _bll.GetAllWaitList();
                gridControlTask.RefreshDataSource();
            }, null);
        }

        private void gridViewTask_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        #region//颜色列表
        RasterColor Color1 = new RasterColor(0, 0, 0);
        RasterColor Color2 = new RasterColor(150, 150, 150);
        RasterColor Color3 = new RasterColor(130, 0, 20);
        RasterColor Color4 = new RasterColor(242, 24, 38);
        RasterColor Color5 = new RasterColor(255, 242, 5);
        RasterColor Color6 = new RasterColor(28, 128, 79);
        RasterColor Color7 = new RasterColor(0, 163, 231);

        RasterColor Color8 = new RasterColor(63, 70, 209);
        RasterColor Color9 = new RasterColor(255, 90, 255);
        RasterColor Color10 = new RasterColor(255, 255, 25);
        RasterColor Color11 = new RasterColor(200, 160, 255);
        RasterColor Color12 = new RasterColor(184, 127, 84);
        RasterColor Color13 = new RasterColor(254, 170, 202);
        RasterColor Color14 = new RasterColor(254, 200, 0);

        RasterColor Color15 = new RasterColor(240, 228, 173);
        RasterColor Color16 = new RasterColor(177, 231, 30);
        RasterColor Color17 = new RasterColor(154, 217, 233);
        RasterColor Color18 = new RasterColor(111, 147, 191);
        RasterColor Color19 = new RasterColor(202, 189, 243);
        RasterColor Color20 = new RasterColor(128, 0, 60);
        RasterColor Color21 = new RasterColor(79, 81, 81);

        RasterColor Color22 = new RasterColor(0, 127, 0);
        RasterColor Color23 = new RasterColor(0, 255, 0);
        RasterColor Color24 = new RasterColor(255, 0, 127);
        RasterColor Color25 = new RasterColor(160, 71, 196);
        RasterColor Color26 = new RasterColor(128, 128, 0);
        RasterColor Color27 = new RasterColor(0, 64, 127);
        RasterColor Color28 = new RasterColor(127, 254, 254);
        RasterColor Color29 = new RasterColor(128, 0, 128);
        RasterColor Color30 = new RasterColor(255, 0, 255);
        #endregion

        private void buttonCommoit_Click(object sender, EventArgs e)
        {
            gridViewTask.PostEditor();
            
            List<ExportTaskInfo> listExportTask = ((List<ExportTaskInfo>)gridViewTask.DataSource).Where(s=>s.IsSelect).ToList();
            if (listExportTask.Count == 0)
            {
                return;
            }
             List<ExportTaskInfo> errorList = _bll.CommitTaskList(listExportTask, userId);
            
            if (errorList.Count == 0)
            {
                MessageBox.Show("全部提交成功");
            }
            else
            {
                
            }
            gridControlTask.DataSource = errorList;
            gridControlTask.RefreshDataSource();
        }

        private void buttonRush_Click(object sender, EventArgs e)
        {
            gridControlTask.DataSource = _bll.GetAllExportList();
            gridControlTask.RefreshDataSource();
        }

        private void linkLabelSelctAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            List<ExportTaskInfo> list = ((List<ExportTaskInfo>)gridViewTask.DataSource);
            list.ForEach(eh => eh.IsSelect = true);
            gridControlTask.RefreshDataSource();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void linkLabelReverse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            List<ExportTaskInfo> list = ((List<ExportTaskInfo>)gridViewTask.DataSource);
            list.ForEach(eh => eh.IsSelect = !eh.IsSelect);
            gridControlTask.RefreshDataSource();
        }

        private void buttonReExport_Click(object sender, EventArgs e)
        {
            List<ExportTaskInfo> listExportTask = ((List<ExportTaskInfo>)gridViewTask.DataSource).Where(s => s.IsSelect).ToList();
            if (listExportTask.Count == 0)
            {
                return;
            }
            List<ExportTaskInfo> errorList = _bll.ReExportTaskList(listExportTask, userId);

            if (errorList.Count == 0)
            {
                MessageBox.Show("全部提交成功");
            }
            else
            {
                string errorStr = "";
                errorList.ForEach(each => {
                    errorStr += "," + each.OrderProductId+"@"+each.CommintResult;

                });
                MessageBox.Show("错误代码" + errorStr);
            }
            gridControlTask.DataSource = errorList;
            gridControlTask.RefreshDataSource();
        }
    }


}
